#!/usr/bin/bash
func_expect(){
 /usr/bin/expect -c "
    set timeout 90;
    spawn $*;
    expect {
          \"(yes/no)?\" {send \"yes\r\";exp_continue}
    	  \"*ssword:\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "
}

func_expect_yozo(){
    cmd2="sudo dpkg -i /home/lxj/Desktop/yozo-office_8.0.1331.101ZH.S1_amd64.deb"
 /usr/bin/expect -c "
    set timeout 30;
    spawn ssh -t $user@$ip $cmd2
    expect {
          \"[Y/n]\" {send \"yes\r\";exp_continue}
          \"(yes/no)?\" {send \"yes\r\";exp_continue}
    	      \"*ssword:\" {send \"$pass\r\";exp_continue}
          \"*密码：\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "
}

func_expect_infree(){
    cmd2="sudo dpkg -i /home/lxj/Desktop/infreeMeeting_1.0.4_all.deb"
 /usr/bin/expect -c "
    set timeout 30;
    spawn ssh -t $user@$ip $cmd2
    expect {
          \"(yes/no)?\" {send \"yes\r\";exp_continue}
    	      \"*ssword:\" {send \"$pass\r\";exp_continue}
          \"*密码：\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "
}
func_expect_infree_new_text(){

cmd1="sudo apt-get remove infree*"
 /usr/bin/expect -c "
    set timeout 30;
    spawn ssh -t $user@$ip $cmd1
    expect {
          \"(yes/no)?\" {send \"yes\r\";exp_continue}
    	      \"*ssword:\" {send \"$pass\r\";exp_continue}
          \"*密码：\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "
}

func_expect_infree_new(){

cmd1="sudo apt remove infreemeeting"
 /usr/bin/expect -c "
    set timeout 30;
    spawn ssh -t $user@$ip $cmd1
    expect {
          \"(yes/no)?\" {send \"yes\r\";exp_continue}
          \"Y/n\" {send \"Y\r\";exp_continue}
    	      \"*ssword:\" {send \"$pass\r\";exp_continue}
          \"请输入密码:\" {send \"$pass\r\";exp_continue}
          \"ip3 的密码：\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "

 cmd2="sudo dpkg -i /home/uos/infree*.deb"
 /usr/bin/expect -c "
    set timeout 30;
    spawn ssh -t $user@$ip $cmd2
    expect {
          \"(yes/no)?\" {send \"yes\r\";exp_continue}
    	      \"*ssword:\" {send \"$pass\r\";exp_continue}
          \"请输入密码:\" {send \"$pass\r\";exp_continue}
          \"ip3 的密码：\" {send \"$pass\r\";exp_continue}
          #\"*~]\$\" {send \"df -h\r exit\r\";interact}
	}
 "

}

func_scp_config(){
    configcmd="scp /home/lxj/myqt/project1009/releaseBuild/QtMeetingApp $2@$3:$4"
    func_expect $configcmd

}

func_scp_dev(){
    scpDebCmd="scp *.deb $2@$3:/home/uos"
    func_expect $scpDebCmd
    #func_expect_infree_new_text
    func_expect_infree_new
}

func_exec(){
 command="scp $1 $2@$3:$4"
 echo -e "\033[36m@@@@@@@@@@@@@@@@@@@@@@@@命    令 : $command\033[0m" 
 func_expect $command
 command2="scp yozo-office_8.0.1331.101ZH.S1_amd64.deb $2@$3:$4"
    func_expect $command2
}

echo '                                                    '
if [ $# -ne 2 ];then
 echo -e "\nwrong call !\nfor eample: $0 file cfg\n"
 exit 1
else
 sdir='.'
 sfile=$sdir/$1
 echo -e "\033[31m########################目    录 : $sdir\033[0m"	
 echo -e "\033[31m########################文    件 : $sfile\033[0m"	
 echo -e "\033[31m########################参数个数 : $#\033[0m"	
 cat $2 | grep -v '^#' | awk '{print $1,$2,$3,$4,$5}' | while read type ip user pass tdir 
  do
   echo '============================================================'
   #func_exec $sfile $user $ip $tdir
    #func_expect_yozo
    #func_expect_infree
    func_scp_dev $sfile $user $ip $tdir
   #sleep 1
  done
fi
echo '                                                    '
